<?php

	class Publication{
		private $id;
		private $idUser;
		private $regDate;
		private $title;
		private $textContent;
		private $idSubCategory;
		private $flComment;
		private $iniDate;
		private $endDate;
		
		
		function __construct($id,$idUser,$regDate,$title,$textContent,$idSubCategory,$flComment,$iniDate,$endDate){
			$this -> id				= $id;
			$this -> idUser			= $idUser;
			$this -> regDate		= $regDate;
			$this -> title			= $title;
			$this -> textContent	= $textContent;
			$this -> idSubCategory	= $idSubCategory;
			$this -> flComment		= $flComment;
			$this -> iniDate		= $iniDate;
			$this -> endDate		= $endDate;			
		}
		
		function getId(){
			return $this-> id;
		}
		
		function setIdUser($idUser){
			$this -> idUser=$idUser;
		}
		
		function getIdUser(){
			return $this -> idUser;
		}
		
		function setRegDate($regDate){
			$this -> regDate=$regDate;
		}
		
		function getRegDate(){
			return $this -> regDate;
		}
		
		function setTitle($title){
			$this -> title=$title;
		}
		
		function getTitle(){
			return $this -> title;
		}
		
		function setTextContent($textContent){
			$this -> textContent=$textContent;
		}
		
		function getTextContent(){
			return $this -> textContent;
		}
		
		function setIdSubCategory($idSubCategory){
			$this -> idSubCategory=$idSubCategory;
		}
		
		function getIdSubCategory(){
			return $this -> idSubCategory;
		}
		
		function setFlComment($flComment){
			$this -> flComment=$flComment;
		}
		
		function getFlComment(){
			return $this -> flComment;
		}
		
		function setIniDate($iniDate){
			$this -> iniDate=$iniDate;
		}
		
		function getIniDate(){
			return $this -> iniDate;
		}
		
		function setEndDate($endDate){
			$this -> endDate=$endDate;
		}
		
		function getEndDate(){
			return $this -> endDate;
		}
		
		
		function getList($db){
			$result=$db->query("SELECT * FROM publication",2);
			return $result;
		}
		
		function getMyList($db,$id){
			$result=$db->query("SELECT * FROM publication WHERE pub_id_user = $id",2);
			return $result;
		}
		
		
		function getPublication($db,$id){
			$result=$db->query("SELECT * FROM publication WHERE pub_IDpublication = $id",2);
			$publication=new Publication(	$result[0]['pub_IDpublication'],
					$result[0]['pub_id_user'],
					$result[0]['pub_regDate'],
					$result[0]['pub_title'],
					$result[0]['pub_textContent'],
					$result[0]['pub_id_subcategory'],
					$result[0]['pub_fl_comment'],
					$result[0]['pub_iniDate'],
					$result[0]['pub_endDate']);
			
			return $publication;
		}
		
		function insert($db,$Publication){
			$idUser			= $Publication -> getIdUser();
			$regDate		= $Publication -> getRegDate();
			$title			= $Publication -> getTitle();
			$textContent	= $Publication -> getTextContent();
			$idSubCategory	= $Publication -> getIdSubCategory();
			$flComment		= $Publication -> getFlComment();
			$iniDate		= $Publication -> getIniDate();
			$endDate		= $Publication -> getEndDate();
			
			$result= $db->insert("INSERT INTO publication 
								  (pub_id_user,pub_regDate,pub_title,pub_textContent,pub_id_subcategory,pub_fl_comment,pub_iniDate,pub_endDate)
								  VALUES($idUser,now(),'$title','$textContent',$idSubCategory,$flComment,'$iniDate','$endDate')");
			return $result;
		}
		
		function delete($db,$Publication){
			$id 	= $Publication -> getId();
			$result = $db->delete("DELETE FROM publication WHERE pub_IDpublication= $id");
			return $result;
		}
		
		function update($db,$Publication){
			$id				= $Publication -> getId();
			$idUser			= $Publication -> getIdUser();
			$regDate		= $Publication -> getRegDate();
			$title			= $Publication -> getTitle();
			$textContent	= $Publication -> getTextContent();
			$idSubCategory	= $Publication -> getIdSubCategory();
			$flComment		= $Publication -> getFlComment();
			$iniDate		= $Publication -> getIniDate();
			$endDate		= $Publication -> getEndDate();
			
			$isNull=is_null($iniDate);
			if ($isNull) {
				$iniDate="NULL";
			}
			else{
				$iniDate="'".iniDate."'";
			}
				
			$isNull=is_null($endDate);
			if ($isNull) {
				$endDate="NULL";
			}
			else{
				$endDate="'".iniDate."'";
					
			}
			
			$result = $db->update("UPDATE publication SET
								   pub_id_user=$idUser,
								   pub_regDate=$regDate,
								   pub_title=$title,
								   pub_textContent=$textContent,
								   pub_id_subcategory=$idSubCategory,
								   pub_fl_comment=$flComment,
								   pub_iniDate=$iniDate,
								   pub_endDate=$endDate
								   WHERE pub_IDpublication= $id");
			return $result;
		}
		
		
	}
?>